package com.daohang361.imqq.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class MyUtil {
	public  static int bytes2Integer(byte[] b) {
		String hex=Hex2Bytes.bytesToHexString(b);
		return Integer.parseInt(hex, 16);
     }
	/**
	 * 转换4字节byte数组为ip地址
	 * @param b
	 * @return
	 * @throws Exception
	 */
	public  static String bytes2IP(byte[] b) throws Exception {
		if(b.length<4||b.length>4)
			throw new Exception("the ip address should have 4 bytes");
		String ip="";
		for(int i=0;i<4;i++){
			if(i<3)
				ip+=byte2Int(b[i])+".";
			else
				ip+=byte2Int(b[i])+"";	
		}
		return ip;
     }
	/**
	 * 转换4字节byte数组为时间
	 * @param b
	 * @return
	 * @throws Exception
	 */
	public  static String bytes2Time(byte[] b) throws Exception {
		if(b.length<4||b.length>4)
				throw new Exception("the time  should have 4 bytes");
		int   time   =   (b[0]  <<   24)   &   0xFF000000   | (b[1]   <<   16)   &   0xFF0000   | 
		(b[2]   <<   8)   &   0xFF00   | (b[3])   &   0xFF; 
		Calendar   calendar   =   new   GregorianCalendar(); 
		calendar.setTimeInMillis(time   *   1000L); 
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd HH:mm:ss"); 
        return sdf.format(calendar.getTime()); 
     }
	/**
	 * 
	 * @return 当前4字节hex时间
	 */
	@SuppressWarnings("static-access")
	public static String Get4HexTime(){
		Calendar   calendar   =   new   GregorianCalendar(); 
		int time=(int) (calendar.getInstance().getTimeInMillis()/1000);
		String timeHex = MyUtil.intTo2Hex(time);
		return timeHex.toUpperCase();
	}
	
	public static String intTo2Hex(int in)
	{
		String result = Integer.toHexString(in);
		if(result.length()==1)
			result="000"+result;
		else if(result.length()==2)
			result="00"+result;
		return result;
	}
	
	/**
	 * 返回2字节或者4字节
	 * @param in
	 * @return
	 */
	public static String intToHex(int in)
	{
		String result = Integer.toHexString(in);
		if(result.length()%2==1)
			result="0"+result;
		
		return result.toUpperCase();
	}
	
	public static int byte2Int(byte b){
		byte[] tempb = new byte[1];
		tempb[0]=b;
		String hex=Hex2Bytes.bytesToHexString(tempb);
		
		return Integer.parseInt(hex, 16);
		
	}
	/**
	 * 简单生成个随机数
	 * @param length
	 * @return
	 */
	public static byte[] getRandom(int length)
	{
		byte[] random = new byte[length];
		for(int i=0;i<length;i++)
		{
			random[i]= (byte) ((int)(Math.random()*256));
		}
		return random;
	}
	/**
	 * 将int转换成4字节的int数组
	 * @param i
	 * @return
	 */
	 public static byte[] int2Bytes(int i) {
	        byte[] abyte0 = new byte[4];
	        abyte0[0] = (byte) (0xff & i);
	        abyte0[1] = (byte) ((0xff00 & i) >> 8);
	        abyte0[2] = (byte) ((0xff0000 & i) >> 16);
	        abyte0[3] = (byte) ((0xff000000 & i) >> 24);
	        return abyte0;
	    }

	public static void main(String a[]){
//		long t =255;
//		t=t*2<<(8*(3-0));
//		System.out.println(t);
		System.out.println(intToHex(256));
	}
}
